//
//  MHRootLeftViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/24.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//


#import "MHRootLeftViewController.h"
#import "MHMyselfHeaderView.h"
#import "MHBaseViewController.h"
#import "MHRootLeftCell.h"
#import "MHLoginViewController.h"
#import "MHMySettingsViewController.h"
#import <UIImageView+WebCache.h>
#import "MHChangePasswordViewController.h"
#import "MHChangeInformationViewController.h"
#import "MHUserInfoSington.h"
#import "UIImage+Addiction.h"
#import "MHCommentVCViewController.h"

@interface MHRootLeftViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) MHBaseViewController *baseVC; //

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) MHMyselfHeaderView *headerView; // 表头View

@property (nonatomic, strong) NSArray *titleArr; // 标题数组
@property (nonatomic, strong) NSArray *iconArr; // 图标数组

@property (nonatomic, strong) NSArray *classArray; // 存放控制器名的数组
@property (nonatomic, strong) MHUserInfo *userInfo;

@end

@implementation MHRootLeftViewController



- (NSArray *)classArray
{
    if (!_classArray) {
        _classArray = @[@"MHMyReadVC",@"MHMyCollectionVC",@"MHCommentVCViewController",@"MHMySettingsViewController"];
    }
    return _classArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *bgImg = [[UIImageView alloc]initWithFrame:self.view.bounds];
    bgImg.image = [UIImage imageNamed:@"IMG_1281.jpg"];
    [self.view addSubview:bgImg];
    
    _titleArr = @[@"最近阅读",@"我的收藏",@"我的评论",@"我的设置"];
    _iconArr = @[@"iconfont-shijian",@"iconfont-xing",@"iconfont-xiazai (1)", @"cascasac"];
    _headerView = [[MHMyselfHeaderView alloc]initWithFrame:CGRectMake(0, 20, Width* 0.66, Height * 0.3)];
    _headerView.backgroundColor = [UIColor clearColor];
    
    // 判断登录状态
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    if ([userD boolForKey:@"isLogin"]) {
        _headerView.loginOrNameLabel.text = [MHUserInfoSington sharedInstance].userInfo.UserNick;
        _headerView.descLabel.text = @"点击修改个人资料哦~";
    }
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
     _tableView.tableHeaderView = _headerView;
    
    UIView *footerView = [[UIView alloc]init];
    _tableView.tableFooterView = footerView;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //去掉分隔线
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[MHRootLeftCell class] forCellReuseIdentifier:@"cell"];
    
    _tableView.rowHeight = 60;
    
    [self.view addSubview:_tableView];
    
    
    [self.headerView.loginBtn addTarget:self action:@selector(didGotoLoginViewController) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logout) name:@"logout" object:nil];
    
}

- (void)logout
{
    if ([MHUserInfoSington sharedInstance].userInfo.icon != nil) {
        _headerView.iconImg.image = [UIImage circleImageWithImage:[MHUserInfoSington sharedInstance].userInfo.icon borderWidth:0 borderColor:[UIColor clearColor]];
    }
    else
    {
        _headerView.iconImg.image = [UIImage circleImageWithImage:[UIImage imageNamed:@"akalin.jpg"] borderWidth:0 borderColor:[UIColor clearColor]];
    }
    _headerView.loginOrNameLabel.text = [MHUserInfoSington sharedInstance].userInfo.UserNick;
}


// 前往loginview  这里要先判断登陆状态。如果是登陆状态就显示修改界面。
- (void)didGotoLoginViewController
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    if ([user boolForKey:@"isLogin"]) {
        MHChangeInformationViewController *changeInformationVC = [[MHChangeInformationViewController alloc]init];
        changeInformationVC.userInfo = [MHUserInfoSington sharedInstance].userInfo;
        changeInformationVC.changeHeaderBlock = ^()
        {
            [self changeHeaderView];
        };
        if (self.rootLeftBlock != nil) {
            self.rootLeftBlock(changeInformationVC);
        }
    }
    else
    {
        // 进入登陆页面
        MHLoginViewController *loginVC = [[MHLoginViewController alloc]init];
        // 从login界面接收的block
        __weak MHRootLeftViewController *test = self;
        loginVC.passUserInfoBlock = ^(MHUserInfo *userInfo){
            test.userInfo = userInfo;
            test.headerView.loginOrNameLabel.text = userInfo.UserNick;
            [test.headerView.iconImg sd_setImageWithURL:[NSURL URLWithString:userInfo.UserPhotoURL]];
            test.headerView.descLabel.text = @"点击修改个人资料";
            
        };
        UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:navi animated:YES completion:^{}];
        
        
    }
}

- (void)changeHeaderView
{
    _headerView.loginOrNameLabel.text = @"登陆 | 注册";
    _headerView.descLabel.text = @"登陆有很多好处哦~";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([MHUserInfoSington sharedInstance].userInfo.icon != nil) {
        _headerView.iconImg.image = [UIImage circleImageWithImage:[MHUserInfoSington sharedInstance].userInfo.icon borderWidth:0 borderColor:[UIColor clearColor]];
    }
    else
    {
        _headerView.iconImg.image = [UIImage circleImageWithImage:[UIImage imageNamed:@"akalin.jpg"] borderWidth:0 borderColor:[UIColor clearColor]];
    }
    _headerView.loginOrNameLabel.text = [MHUserInfoSington sharedInstance].userInfo.UserNick;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MHRootLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.titleLabel.text = self.titleArr[indexPath.row];
    cell.iconView.image = [UIImage imageNamed:self.iconArr[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     
    _baseVC = [[NSClassFromString(self.classArray[indexPath.row]) alloc]init];
    if (self.rootLeftBlock != nil) {
        self.rootLeftBlock(_baseVC);
    }
}



@end
